package be.ipl.finito.domaine;

import java.util.List;

import javax.ejb.EJBException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import be.ipl.finito.exceptions.ArgumentInvalideException;
import be.ipl.finito.exceptions.JoueurDejaInscritException;
import be.ipl.finito.exceptions.JoueurInExistantException;
import be.ipl.finito.exceptions.PartieInExistanteException;
import be.ipl.finito.usecases.GestionJoueur;
import be.ipl.finito.usecases.GestionPartie;
import be.ipl.finito.usecasesimpl.GestionPartieImpl;

public class TestPlateau {

	private static Context ctx;
	private static GestionJoueur gestionJoueur;
	private static GestionPartie gestionPartie;
	Plateau p;
	@BeforeTest
	public void setUp() throws NamingException {
		p = new Plateau();

//		ctx = new InitialContext();
//		gestionJoueur = (GestionJoueur) ctx.lookup("ejb:/FINITO_EJB/GestionJoueurImpl!be.ipl.finito.usecases.GestionJoueur");
//		gestionPartie = (GestionPartie) ctx.lookup("ejb:/FINITO_EJB/GestionPartieImpl!be.ipl.finito.usecases.GestionPartie");
	}
	
	 @AfterMethod
	    public void tearDown() {
		 p = new Plateau();
	  }
	 
	@Test
	public void testerAjouterJeton1(){
		Case case1 = new Case(1);
		Jeton jeton1 = new Jeton(1, 1);
//		p.ajouterJeton(case1, jeton1);
		case1.setJeton(jeton1);
		p.ajouterJeton(case1);
		List<Case> lescases = p.getLesCases();
		Assert.assertEquals(lescases.size(), 1);
	}
	
	
	@Test
	public void testerAjouterJeton1bis(){
		Case case1 = new Case(1);
		Jeton jeton1 = new Jeton(1, 1);
//		p.ajouterJeton(case1, jeton1);
		case1.setJeton(jeton1);
		p.ajouterJeton(case1);
		List<Case> lescases = p.getLesCases();
		Assert.assertTrue(lescases.contains(case1));
	}
	
	@Test
	public void testerAjouterJeton2(){
		Case case1 = new Case(1);
		Case case2 = new Case(2);
		Jeton jeton1 = new Jeton(1, 1);
		Jeton jeton2 = new Jeton(2, 2);
//		p.ajouterJeton(case1, jeton1);
//		p.ajouterJeton(case2, jeton2);
		case1.setJeton(jeton1);
		case2.setJeton(jeton2);
		p.ajouterJeton(case1);
		p.ajouterJeton(case2);
		List<Case> lescases = p.getLesCases();
		Assert.assertEquals(lescases.size(), 2);
	}
	
	@Test
	public void testerAjouterJeton2bis(){
		Case case1 = new Case(1);
		Case case2 = new Case(2);

		Jeton jeton1 = new Jeton(1, 1);
		Jeton jeton2 = new Jeton(2, 2);
		case1.setJeton(jeton1);
		case2.setJeton(jeton2);
//		p.ajouterJeton(case1, jeton1);
//		p.ajouterJeton(case2, jeton2);
		p.ajouterJeton(case1);
		p.ajouterJeton(case2);
		List<Case> lescases = p.getLesCases();
		Assert.assertTrue(lescases.contains(case1) && lescases.contains(case2));
	}
	
	
	@Test
	public void testerCalculerScore(){
		Case case1 = new Case(1);
		Case case2 = new Case(2);
		Case case3 = new Case(3);
		
		Jeton jeton = new Jeton(1, 1);
		Jeton jeton2 = new Jeton(2, 2);
		Jeton jeton3 = new Jeton(3, 3);
		case1.setJeton(jeton);
		case2.setJeton(jeton2);
		case3.setJeton(jeton3);
//		p.ajouterJeton(case1, jeton);
//		p.ajouterJeton(case2, jeton2);
//		p.ajouterJeton(case3, jeton3);
		p.ajouterJeton(case1);
		p.ajouterJeton(case2);
		p.ajouterJeton(case3);
		System.out.println("LESCORE :" + p.calculerScore());
		Assert.assertEquals(p.calculerScore(), 3);
	}
	
	
	@Test
	public void testerIsAutorised()throws ArgumentInvalideException{
//		Joueur joueur = new Joueur("michou", "password", "debacker", "michel", "michel.debacker@ipl.be");
//		Partie unePartie = new Partie("la partie de michou");
//		try {
//			gestionPartie.rejoindrePartie(unePartie.getId(), joueur);
//		} catch (IllegalStateException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (PartieInExistanteException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (JoueurInExistantException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (JoueurDejaInscritException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		

		
		
		p.getLesCases().add(new Case(0));
		List<Integer> li =p.isAutorised(0);
		Assert.assertEquals(li.get(0).intValue(),1);
		
		
//		System.out.println(li.toString());
	}
	
	@Test
	public void testerIsAutorised2()throws ArgumentInvalideException{
//		Joueur joueur = new Joueur("michou", "password", "debacker", "michel", "michel.debacker@ipl.be");
//		Partie unePartie = new Partie("la partie de michou");
//		try {
//			gestionPartie.rejoindrePartie(unePartie.getId(), joueur);
//		} catch (IllegalStateException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (PartieInExistanteException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (JoueurInExistantException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (JoueurDejaInscritException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		

		
	
		List<Integer> li =p.isAutorised(0);
		Assert.assertEquals(li.get(0).intValue(),0);
		
		
//		System.out.println(li.toString());
	}
	
	
	@Test
	public void testerIsAutorised3()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		List<Integer> li =p.isAutorised(1);
		Assert.assertEquals(li.get(0).intValue(),2);
	}
	
	@Test
	public void testerIsAutorised4()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(2));
		Assert.assertEquals(li.get(0).intValue(),3);
	}
	@Test
	public void testerIsAutorised5()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(6));
		Assert.assertEquals(li.get(0).intValue(),8);
		Assert.assertEquals(li.get(1).intValue(),9);
	}
	
	@Test
	public void testerIsAutorised6()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(4));
		Assert.assertEquals(li.get(0).intValue(),4);
		Assert.assertEquals(li.get(1).intValue(),5);
	}
	
	@Test
	public void testerIsAutorised7()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(5));
		Assert.assertEquals(li.get(0).intValue(),6);
		Assert.assertEquals(li.get(1).intValue(),7);
	}
	
	@Test
	public void testerIsAutorised8()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(7));
		Assert.assertEquals(li.get(0).intValue(),10);
		Assert.assertEquals(li.get(1).intValue(),11);
	}
	
	@Test
	public void testerIsAutorised9()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(8));
		Assert.assertEquals(li.get(0).intValue(),12);
		Assert.assertEquals(li.get(1).intValue(),13);
	}
	@Test
	public void testerIsAutorised10()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(9));
		Assert.assertEquals(li.get(0).intValue(),14);
		Assert.assertEquals(li.get(1).intValue(),15);
	}
	@Test
	public void testerIsAutorised11()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(10));
		Assert.assertEquals(li.get(0).intValue(),16);
		Assert.assertEquals(li.get(1).intValue(),17);
	}
	
	@Test
	public void testerIsAutorised12()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(11));
		Assert.assertEquals(li.get(0).intValue(),18);
		Assert.assertEquals(li.get(1).intValue(),19);
	}
	@Test
	public void testerIsAutorised13()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(12));
		Assert.assertEquals(li.get(0).intValue(),20);
		Assert.assertEquals(li.get(1).intValue(),21);
	}
	
	@Test
	public void testerIsAutorised14()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(13));
		Assert.assertEquals(li.get(0).intValue(),22);
		Assert.assertEquals(li.get(1).intValue(),23);
	}
	
	@Test
	public void testerIsAutorised15()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(18));
		Assert.assertEquals(li.get(0).intValue(),32);
		Assert.assertEquals(li.get(1).intValue(),33);
	}
	@Test
	public void testerIsAutorised16()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(19));
		Assert.assertEquals(li.get(0).intValue(),34);
	}
	@Test
	public void testerIsAutorised17()throws ArgumentInvalideException{		
		p.getLesCases().add(new Case(0));
		p.getLesCases().add(new Case(1));
		p.getLesCases().add(new Case(2));

		List<Integer> li =p.isAutorised(p.MapCasePositionsPourDeInit().get(20));
		Assert.assertEquals(li.get(0).intValue(),35);
	}
	
	
}
